package com.ssyt.store.sys.dao.impl;

import org.springframework.stereotype.Repository;

import com.ssyt.store.common.BaseDao;
import com.ssyt.store.sys.dao.SysUserRoleDao;
import com.ssyt.store.sys.entity.SysUserEntity;
import com.ssyt.store.sys.entity.SysUserRoleEntity;

@Repository
public class SysUserRoleDaoImpl extends BaseDao<SysUserRoleEntity, String>
		implements SysUserRoleDao {

	@Override
	public String getRoleByUserId(String id) {
		return uniqueSinglePropertyByHql(String.class,
				"SELECT sysRoleId FROM SysUserRoleEntity WHERE sysUserId = ?",
				id);
	}

	@Override
	public String addUserRole(String userId, String roleId) {
		return save(new SysUserRoleEntity(userId, roleId));
	}

	@Override
	public void updateUserRole(SysUserEntity user) {
		String hql = "SELECT id FROM SysUserRoleEntity WHERE sysUserId = ?";
		String id = uniqueSinglePropertyByHql(String.class, hql, user.getId());
		SysUserRoleEntity userRole = get(id);
		if (userRole != null) {
			userRole.setSysRoleId(user.getRoleId());
		}
		update(userRole);
	}

}
